<?php


namespace app\admin\controller;

use app\common\controller\CommonService;
use think\facade\Validate;
use think\Controller;
use think\Request;
use think\Db;

class User extends Controller
{
    /**
     * 所有会员
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allUser(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('user_manage')
            ->alias('u')
            ->leftJoin('user_manage parUser','parUser.id = u.par_id')
            ->leftJoin('partner','partner.id = u.partner_id')
            ->leftJoin('user_manage partnerUser','partner.uid = partnerUser.id')
            ->leftJoin('super_partner','super_partner.id = u.super_partner_id')
            ->leftJoin('user_manage superPartnerUser','super_partner.uid = superPartnerUser.id');
        if (!empty($requestData['nickname'])){
            $db = $db->where('u.nickname',$requestData['nickname']);
        }
        if (!empty($requestData['code'])){
            $db = $db->where('u.code',$requestData['code']);
        }
        if (!empty($requestData['tel'])){
            $db = $db->where('u.tel',$requestData['tel']);
        }
        if (!empty($requestData['register_start']) && !empty($requestData['register_end'])){
            $db = $db->whereBetween('u.time',[$requestData['register_start'],$requestData['register_end']]);
        }
        if (!empty($requestData['last_login_start']) && !empty('last_login_end')){
            $db = $db->where('u.last_login_time',[$requestData['last_login_start'],$requestData['last_login_end']]);
        }
//        if (!empty($requestData['state'])){
//            $db = $db->where('u.state',$requestData['state']);
//        }
        if (!empty($requestData['tb_openid'])) {
            if ($requestData['tb_openid'] == 1) {
                $db = $db->whereNotNull('u.tb_openid');
            } else {
                $db = $db->whereNull('u.tb_openid');
            }
        }
        if (!empty($requestData['alipay_id'])) {
            if ($requestData['alipay_id'] == 1) {
                $db = $db->whereNotNull('u.alipay_id');
            } else {
                $db = $db->whereNull('u.alipay_id');
            }
        }
        if (!empty($requestData['is_vip'])) {
            if ($requestData['is_vip'] == 1) {
                $db = $db->where('u.is_vip', 'NEQ', 0);
            } else {
                $db = $db->where('u.is_vip', 0);
            }
        }
        //推荐人邀请码
        if (!empty($requestData['par_code'])) {
            $parId = Db::name('user_manage')->where('code',$requestData['par_code'])->value('id');
            $db = $db->where('u.par_id', $parId);
        }
        //合伙人邀请码
        if (!empty($requestData['partner_code'])){
            $partnerId = Db::name('user_manage')->where('code',$requestData['partner_code'])->value('id');
            $db = $db->where('u.partner_id',$partnerId);
        }
        //超级合伙人邀请码
        if (!empty($requestData['super_partner_code'])){
            $superPartnerId = Db::name('user_manage')->where('code',$requestData['super_partner_code'])->value('id');
            $db = $db->where('u.super_partner_id',$superPartnerId);
        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
            $db = $db->order($requestData['order_type'], $requestData['sort_type']);
        } else {
            $db = $db->order('u.id', 'desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('u.id as user_id,u.nickname,u.headimgurl,u.code as user_code,u.tel,u.time,u.last_login_time,u.alipay_id,u.tb_openid,u.is_vip,u.money,u.jifen,parUser.nickname as par_nickname,parUser.code as par_code,partnerUser.nickname as partner_nickname,partnerUser.code as partner_code,superPartnerUser.nickname as super_partner_nickname,superPartnerUser.code as super_partner_code')
            ->page(input('page', 1), input('limit', 20))
            ->select();
        foreach ($data['data'] as $k => $v){
            $date['data'][$k]['nickname'] = !empty($v['nickname']) ? CommonService::emoji_decode($v['nickname']) : '';
            $date['data'][$k]['par_nickname'] = !empty($v['par_nickname']) ? CommonService::emoji_decode($v['par_nickname']) : '';
            $date['data'][$k]['partner_nickname'] = !empty($v['partner_nickname']) ? CommonService::emoji_decode($v['partner_nickname']) : '';
            $date['data'][$k]['super_partner_nickname'] = !empty($v['super_partner_nickname']) ? CommonService::emoji_decode($v['super_partner_nickname']) : '';
            $date['data'][$k]['tb_openid'] = empty($v['tb_openid']) ? '否' : '是';
            $date['data'][$k]['alipay_id'] = empty($v['alipay_id']) ? '否' : '是';
            $date['data'][$k]['is_vip'] = $v['is_vip'] == 0 ? '否' : '是';
            $tb_num = Db::name('tb_order')->where('order_status', 'NEQ', 0)->where('order_type', 1)->where('is_weiquan', 0)->where('uid',$v['user_id'])->count();
            $jd_num = Db::name('jd_order')->where('order_status', 'NEQ', 0)->where('uid',$v['user_id'])->count();
            $pdd_num = Db::name('pdd_order')->where('order_status', 'NEQ', 0)->where('uid',$v['user_id'])->count();
            $date['data'][$k]['order_num'] = $tb_num + $jd_num + $pdd_num;
            $hongbao = Db::name('hongbao')->where('uid',$v['user_id'])->sum('hongbao_money');
            $date['data'][$k]['hongbao_num'] =  isset($hongbao) ? $hongbao : 0;
            $date['data'][$k]['invite_num'] = Db::name('user_manage')->where('par_id',$v['user_id'])->count();
        }

        $data['total_num'] = $db1->count();
        return CommonService::success($data);
    }

    /**
     * 会员详情
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneUser(Request $request)
    {
        $requestData = $request->post();
        $validate = Validate::make([
            'uid|用户id' => 'require|number',
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $oneUserInfo = Db::name('user_manage')
            ->alias('u')
            ->leftJoin('user_manage parUser','parUser.id = u.par_id')
            ->leftJoin('partner','partner.id = u.partner_id')
            ->leftJoin('user_manage partnerUser','partner.uid = partnerUser.id')
            ->leftJoin('super_partner','super_partner.id = u.super_partner_id')
            ->leftJoin('user_manage superPartnerUser','super_partner.uid = superPartnerUser.id')
            ->field('u.id as user_id,u.headimgurl,u.nickname,u.code as user_code,u.sex,u.shengri,u.zhiye,u.register_province,u.register_city,u.vip,u.tel,u.alipay_id,u.tb_openid,u.money,u.jifen,parUser.code as par_code,parUser.nickname as pae_nickname,partner.code as partner_code,partnerUser.nickname as partner_nickname,partnerUser.code as partner_code,superPartnerUser.nickname as super_partner_nickname,superPartnerUser.code as super_partner_code')
            ->where('u.id',$requestData['uid'])
            ->find();
        if ($oneUserInfo['user_id'] != $requestData['uid']){
            return CommonService::error(1);
        }
        $oneUserInfo['nickname'] = !empty($oneUserInfo['nickname']) ? CommonService::emoji_decode($oneUserInfo['nickname']) : '';
        $oneUserInfo['par_nickname'] = !empty($oneUserInfo['par_nickname']) ? CommonService::emoji_decode($oneUserInfo['par_nickname']) : '';
        $oneUserInfo['partner_nickname'] = !empty($oneUserInfo['partner_nickname']) ? CommonService::emoji_decode($oneUserInfo['partner_nickname']) : '';
        $oneUserInfo['super_partner_nickname'] = !empty($oneUserInfo['super_partner_nickname']) ? CommonService::emoji_decode($oneUserInfo['super_partner_nickname']) : '';

        //累计返利
        $tbPrice = Db::name('tb_order')
            ->where('order_type', 1)
            ->where('order_status', '<>', 0)
            ->where('is_weiquan', 0)
            ->where('is_rebate',1)
            ->where('uid',$oneUserInfo['uid'])
            ->sum('user_rebate_fee');
        $jdPrice = Db::name('jd_order')
            ->where('order_status', '<>', 0)
            ->where('uid',$oneUserInfo['uid'])
            ->sum('user_rebate_fee');
        $pddPrice = Db::name('pdd_order')
            ->where('order_status', '<>', 0)
            ->where('uid',$oneUserInfo['uid'])
            ->sum('user_rebate_fee');
        $oneUserInfo['user_rebate_price'] = $tbPrice + $jdPrice + $pddPrice;

        //累计赚钱

        //累计提现
        $oneUserInfo['all_Withdrawal_price'] = Db::name('liushui')
            ->where('lb',5)
            ->where('uid',$oneUserInfo['uid'])
            ->sum('money');

        //有效订单
        $tbOrder = Db::name('tb_order')
            ->where('order_type', 1)
            ->where('order_status', '<>', 0)
            ->where('is_weiquan', 0)
            ->where('uid',$oneUserInfo['uid'])
            ->count();
        $jdOrder = Db::name('jd_order')
            ->where('order_status', '<>', 0)
            ->where('uid',$oneUserInfo['uid'])
            ->count();
        $pddOrder = Db::name('pdd_order')
            ->where('order_status', '<>', 0)
            ->where('uid',$oneUserInfo['uid'])
            ->count();
        $oneUserInfo['order_num'] = $tbOrder + $jdOrder + $pddOrder;

        //邀请好友数
        $oneUserInfo['invite_num'] = Db::name('user_manage')->where('par_id',$oneUserInfo['user_id'])->count();

        //可用红包
        $oneUserInfo['not_used_hongbao'] = $this->getHongbao($oneUserInfo['uid'],0);
        //累计红包
        $oneUserInfo['all_hongbao'] = $this->getHongbao($oneUserInfo['uid']);
        //累计使用红包
        $oneUserInfo['used_hongbao'] = $this->getHongbao($oneUserInfo['uid'],1);

        //累计金币
        $oneUserInfo['all_jifen'] = Db::name('jifen_liushui')->where('jifen','>',0)->where('uid',$oneUserInfo['uid'])->sum('jifen');
        //累计使用积分
        $oneUserInfo['used_jifen'] = Db::name('jifen_liushui')->where('jifen','<',0)->where('uid',$oneUserInfo['uid'])->sum('jifen');

        //终端设备

    }


    /**
     * 禁用会员(数据库结构需要添加字段，需要添加禁用表)
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function ableUser(Request $request)
     {
         $requestData = $request->post();
         $validate = Validate::make([
             'uid|用户id' => 'require|number',
             'state|状态' => 'require|number'
         ]);
         if (!$validate->check($requestData)) {
             return CommonService::error($validate->getError());
         }
         $userInfo = Db::name('user_manage')->where('id',$requestData['uid'])->find();
         if (!$userInfo){
             return CommonService::error(1);
         }
         Db::name('user_manage')->where('id', $requestData['uid'])->update([
             'state' => $requestData['state'],
         ]);
         Db::name('user_status_log')->insert([
             'uid' => $requestData['uid'],
             'status' => $requestData['state'],
             'remark' => $requestData['remark'],
             'time' => time()
         ]);
         return CommonService::success([]);
     }

    /**
     * 赠送红包(红包批次字段 添加)
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function giveHongbao(Request $request)
    {
        $requestData = $request->post();
        $validate = Validate::make([
            'uid|用户id' => 'require|number',
            'num|红包数量' => 'require|number',
            'hongbao_batch' => 'require'
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $userInfo = Db::name('user_manage')->where('id',$requestData['uid'])->find();
        if (!$userInfo){
            return CommonService::error(1);
        }
        Db::name('hongbao')->where('id', $requestData['uid'])->update([

        ]);
    }

    /**
     * 调整账户余额
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function editBalance(Request $request)
     {
         $requestData = $request->post();
         $validate = Validate::make([
             'uid|用户id' => 'require|number',
             'money|调整金额' => 'require|number',
         ]);
         if (!$validate->check($requestData)) {
             return CommonService::error($validate->getError());
         }
         $userInfo = Db::name('user_manage')->where('id',$requestData['uid'])->find();
         if (!$userInfo){
             return CommonService::error(1);
         }
         $money = $userInfo['money'] + $requestData['money'];
         if ($money <= 0){
             $moneyMeb = 0;
         }
         if ($money > 0){
             $moneyMeb = $money;
         }
         Db::name('user_manage')->where('id', $requestData['uid'])->update([
             'money' => $moneyMeb,
         ]);
         Db::name('liushui')->insert([
             'uid' => $requestData['uid'],
             'money' => $money,
             'yue' => $moneyMeb,
             'lb' => 4,
             'time' => time()
         ]);
         return CommonService::success([]);
     }

    /**
     * 调整会员金币数量
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function editJifen(Request $request)
     {
         $requestData = $request->post();
         $validate = Validate::make([
             'uid|用户id' => 'require|number',
             'jifen|调整金币' => 'require|number',
         ]);
         if (!$validate->check($requestData)) {
             return CommonService::error($validate->getError());
         }
         $userInfo = Db::name('user_manage')->where('id',$requestData['uid'])->find();
         if (!$userInfo){
             return CommonService::error(1);
         }
         $jifen = $requestData['jifen'] + $userInfo['jifen'];
         if ($jifen <= 0){
             $jifenMeb = 0;
         }
         if ($jifen > 0){
             $jifenMeb = $jifen;
         }
         Db::name('user_manage')->where('id', $requestData['uid'])->update([
             'jifen' => $jifenMeb,
         ]);
         Db::name('jifen_liushui')->insert([
             'uid' => $requestData['uid'],
             'jifen' => $jifen,
             'later_jifen' => $jifenMeb,
             'type' => 9,
             'time' => time()
         ]);
         return CommonService::success([]);
     }

    /**
     * 提现审核列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function withdrawExamineList(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('withdraw')
            ->alias('w')
            ->leftJoin('user_manage u','u.id = w.uid');

        if (!empty($requestData['nickname'])){
            $db = $db->where('u.nickname',$requestData['nickname']);
        }
        if (!empty($requestData['code'])){
            $db = $db->where('u.code',$requestData['code']);
        }
        if (!empty($requestData['tel'])){
            $db = $db->where('u.tel',$requestData['tel']);
        }
        if (!empty($requestData['start_apply_time']) && !empty($requestData['end_apply_time'])){
            $db = $db->whereBetween('w.apply_time',[$requestData['start_apply_time'],$requestData['end_apply_time']]);
        }
        if (!empty($requestData['status'])){
            $db = $db->where('w.status',$requestData['status']);
        }
        if (!empty($requestData['start_time']) && !empty($requestData['end_time'])){
            $db = $db->whereBetween('w.end_time',[$requestData['start_time'],$requestData['end_time']]);
        }
        if (!empty($requestData['apply_name'])){
            $db = $db->where('w.apply_name',$requestData['apply_name']);
        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
            $db = $db->order($requestData['order_type'], $requestData['sort_type']);
        } else {
            $db = $db->order('w.id', 'desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('u.headimgurl,u.nickname,u.code,u.tel,w.money,w.apply_time,w.status,w.end_time,w.apply_name')
            ->page(input('page', 1), input('limit', 20))
            ->select();
        $data['total_num'] = $db1->count();
        return CommonService::success($data);
    }
    /**
     * 提现审核(多选)
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editwithdraw(Request $request)
    {
        $requestData = $request->post();
        $validate = Validate::make([
            'uid|用户id' => 'require',
            'beizu|审核说明' => 'require',
            'status|审核状态' => 'require|number'
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $uids = explode(',',$requestData['uid']);
        foreach ($uids as $k => $v){
            $data[$k] = [
                'uid' => $v,
                'status' => $requestData['status'],
                'beizu' => $requestData['beizu'],
                'end_time' => time(),
            ];
        }
        Db::name('withdraw')->saveAll($data);
        return CommonService::success([]);
    }

    /**
     * 禁用记录（需要添加表）
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userStatusList(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('user_status_log')
            ->alias('l')
            ->leftJoin('user_manage u','u.id = l.uid');
        if (!empty($requestData['nickname'])){
            $db = $db->where('u.nickname',$requestData['nickname']);
        }
        if (!empty($requestData['code'])){
            $db = $db->where('u.code',$requestData['code']);
        }
        if (!empty($requestData['tel'])){
            $db = $db->where('u.tel',$requestData['tel']);
        }
        if (!empty($requestData['status'])){
            $db = $db->where('l.status',$requestData['status']);
        }
        if (!empty($requestData['start_time']) && !empty($requestData['end_time'])){
            $db = $db->whereBetween('time',[$requestData['start_time'],$requestData['end_time']]);
        }
        if (!empty($requestData['admin_name'])){

        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
            $db = $db->order($requestData['order_type'], $requestData['sort_type']);
        } else {
            $db = $db->order('l.id', 'desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('u.headimgurl,u.nickname,u.code,u.tel,l.status,l.time,l.admin_name,l.remark')
            ->page(input('page', 1), input('limit', 20))
            ->select();
        $data['total_num'] = $db1->count();
        foreach ($data['data'] as $k => $v){
            $data['data'][$k]['nickname'] = !empty($v['nickname']) ? CommonService::emoji_decode($v['nickname']) : '';
        }
        return CommonService::success($data);
    }

    /**
     * 邀请记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userInvitationList(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('huodong_order')
            ->alias('h')
            ->leftJoin('user_manage user','user.unionid = h.unionid')
            ->leftJoin('user_manage parUser','parUser.id = h.par_id');
        if (!empty($requestData['nickname'])){
            $db = $db->where('h.nickname',$requestData['nickname']);
        }
        if (!empty($requestData['par_nickname'])){
            $db = $db->where('parUser.nickname',$requestData['par_nickname']);
        }
        if (!empty($requestData['par_code'])){
            $db = $db->where('parUser.code',$requestData['par_code']);
        }
        if (!empty($requestData['start_time']) && !empty($requestData['end_time'])){
            $db = $db->whereBetween('time',[$requestData['start_time'],$requestData['end_time']]);
        }
        if (!empty($requestData['invite_status'])) {
            if ($requestData['invite_status'] == -1){
                $db = $db->where('h.status', 0);
            }
            if ($requestData['invite_status'] == 1) {
                $db = $db->where('h.status','<>', 0);
            }
        }
        if (!empty($requestData['register_start']) && !empty($requestData['register_end'])) {
            $db = $db->whereBetween('user.time', [$requestData['register_start'], $requestData['register_end']]);
        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
            $db = $db->order($requestData['order_type'], $requestData['sort_type']);
        } else {
            $db = $db->order('h.id', 'desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('h.headimgurl,h.nickname,parUser.nickname as par_nickname,parUser.code as par_code,h.time,h.status as invite_status,user.time as register_time')
            ->page(input('page', 1), input('limit', 20))
            ->select();
        $data['total_num'] = $db1->count();
        foreach ($data['data'] as $k => $v){
            $data['data'][$k]['nickname'] = !empty($v['nickname']) ? CommonService::emoji_decode($v['nickname']) : '';
            $data['data'][$k]['par_nickname'] = !empty($v['par_nickname']) ? CommonService::emoji_decode($v['par_nickname']) : '';
            switch ($v['invite_status']) {
                case 0:
                    $data['data'][$k]['invite_status'] = '邀请中';
                    break;
                case 1:
                    $data['data'][$k]['invite_status'] = '邀请成功';
                    break;
                case 2:
                    $data['data'][$k]['invite_status'] = '邀请超时';
                    break;
            }
        }
        return CommonService::success($data);
    }


    /**
     * 账户流水
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userLiushuiList(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('liushui')
            ->alias('l')
            ->leftJoin('user_manage u','u.id = l.uid');
        if (!empty($requestData['nickname'])){
            $db = $db->where('u.nickname',$requestData['nickname']);
        }
        if (!empty($requestData['code'])){
            $db = $db->where('u.code',$requestData['code']);
        }
        if (!empty($requestData['tel'])){
            $db = $db->where('u.tel',$requestData['tel']);
        }
        if (!empty($requestData['lb'])){
            $db = $db->where('l.lb',$requestData['lb']);
        }
        if (!empty($requestData['start_time']) && !empty($requestData['end_time'])){
            $db = $db->whereBetween('l.time',[$requestData['start_time'],$requestData['end_time']]);
        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
            $db = $db->order($requestData['order_type'], $requestData['sort_type']);
        } else {
            $db = $db->order('l.id', 'desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('u.headimgurl,u.nickname,u.code,u.tel,l.money as l_money,l.lb,l.time as l_time')
            ->page(input('page', 1), input('limit', 20))
            ->select();
        $data['total_num'] = $db1->count();
        foreach ($data['data'] as $k => $v){
            $data['data'][$k]['nickname'] = !empty($v['nickname']) ? CommonService::emoji_decode($v['nickname']) : '';
            if ($v['l_money'] > 0){
                $data['data'][$k]['income'] = $v['l_money'];
                $data['data'][$k]['pay'] = 0;
            }
            if ($v['l_money'] < 0){
                $data['data'][$k]['income'] = 0;
                $data['data'][$k]['pay'] = $v['l_money'];
            }
            switch ($v['lb']) {
                case 1:
                    $date['data'][$k]['lb'] = '淘宝返利';
                    break;
                case 2:
                    $date['data'][$k]['lb'] = '京东返利';
                    break;
                case 3:
                    $date['data'][$k]['lb'] = '活动';
                    break;
                case 4:
                    $date['data'][$k]['lb'] = '平台调整';
                    break;
                case 5:
                    $date['data'][$k]['lb'] = '申请提现';
                    break;
                case 6:
                    $date['data'][$k]['lb'] = '抽奖';
                    break;
                case 7:
                    $date['data'][$k]['lb'] = '拼多多返利';
                    break;
                case 8:
                    $date['data'][$k]['lb'] = '新用户红包';
                    break;
                case 9:
                    $date['data'][$k]['lb'] = '邀请首单奖励';
                    break;
                case 10:
                    $date['data'][$k]['lb'] = '金币兑换红包';
                    break;
                case 11:
                    $date['data'][$k]['lb'] = '一级用户返利';
                    break;
                case 12:
                    $date['data'][$k]['lb'] = '二级用户返利';
                    break;
                case 13:
                    $date['data'][$k]['lb'] = '连续签到红包';
                    break;
                case 14:
                    $date['data'][$k]['lb'] = '订单红包';
                    break;
                default:
                    $data['data'][$k]['lb'] = '其他';
            }
        }
        return CommonService::success($data);
    }


    /**
     * 金币动态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function userJifenList(Request $request)
     {
         $requestData = $request->post();
         $db = Db::name('jifen_liushui')
             ->alias('j')
             ->leftJoin('user_manage u','u.id = j.uid');
         if (!empty($requestData['nickname'])){
             $db = $db->where('u.nickname',$requestData['nickname']);
         }
         if (!empty($requestData['code'])){
             $db = $db->where('u.code',$requestData['code']);
         }
         if (!empty($requestData['tel'])){
             $db = $db->where('u.tel',$requestData['tel']);
         }
         if (!empty($requestData['type'])){
             $db = $db->where('j.type',$requestData['type']);
         }
         if (!empty($requestData['start_time']) && !empty($requestData['end_time'])){
             $db = $db->whereBetween('j.time',[$requestData['start_time'],$requestData['end_time']]);
         }
         if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
             $db = $db->order($requestData['order_type'], $requestData['sort_type']);
         } else {
             $db = $db->order('j.id', 'desc');
         }
         $db1 = clone $db;
         $data['data'] = $db
             ->field('u.headimgurl,u.nickname,u.code,u.tel,j.jifen as j_jifen,j.type,j.time as j_time')
             ->page(input('page', 1), input('limit', 20))
             ->select();
         $data['total_num'] = $db1->count();
         foreach ($data['data'] as $k => $v){
             $data['data'][$k]['nickname'] = !empty($v['nickname']) ? CommonService::emoji_decode($v['nickname']) : '';
             if ($v['j_jifen'] > 0){
                 $data['data'][$k]['income'] = $v['j_jifen'];
                 $data['data'][$k]['pay'] = 0;
             }
             if ($v['j_jifen'] < 0){
                 $data['data'][$k]['income'] = 0;
                 $data['data'][$k]['pay'] = $v['j_jifen'];
             }
             switch ($v['type']) {
                 case 1:
                     $date['data'][$k]['type'] = '新用户';
                     break;
                 case 2:
                     $date['data'][$k]['type'] = '签到';
                     break;
                 case 3:
                     $date['data'][$k]['type'] = '分享';
                     break;
                 case 4:
                     $date['data'][$k]['type'] = '邀请';
                     break;
                 case 5:
                     $date['data'][$k]['type'] = '抽奖';
                     break;
                 case 6:
                     $date['data'][$k]['type'] = '中奖';
                     break;
                 case 7:
                     $date['data'][$k]['type'] = '兑换红包';
                     break;
                 case 8:
                     $date['data'][$k]['type'] = '补充用户信息';
                     break;
                 case 9:
                     $date['data'][$k]['type'] = '平台调整';
                     break;
                 case 10:
                     $date['data'][$k]['type'] = '搜索标题';
                     break;
                 case 11:
                     $date['data'][$k]['type'] = '浏览商品';
                     break;
                 case 12:
                     $date['data'][$k]['type'] = '分享商品';
                     break;
                 case 13:
                     $date['data'][$k]['type'] = '绑定手机';
                     break;
                 case 14:
                     $date['data'][$k]['type'] = '学习教程';
                     break;
                 case 15:
                     $date['data'][$k]['type'] = '新人首单';
                     break;
             }
         }
         return CommonService::success($data);
     }

    /**
     * 红包记录（需要增加红包批次字段 honghao_batch，is_use字段增加了 ：2 强制失效）
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userHongbaoList(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('hongbao')
            ->alias('h')
            ->leftJoin('user_manage u','u.id = h.uid');
        if (!empty($requestData['nickname'])){
            $db = $db->where('u.nickname',$requestData['nickname']);
        }
        if (!empty($requestData['code'])){
            $db = $db->where('u.code',$requestData['code']);
        }
        if (!empty($requestData['tel'])){
            $db = $db->where('u.tel',$requestData['tel']);
        }
        if (!empty($requestData['honghao_batch'])){
            $db = $db->where('h.honghao_batch',$requestData['honghao_batch']);
        }
        if (!empty($requestData['be_from'])){
            $db = $db->where('h.be_from',$requestData['be_from']);
        }
        if (!empty($requestData['is_use'])){
            if ($requestData['is_use'] = 3){
                $db = $db->where('h.end_use_time','<',time());
            }else{
                $db = $db->where('h.is_use',$requestData['is_use']);
            }
        }
        if (!empty($requestData['start_exchange_time']) && !empty($requestData['end_exchange_time'])){
            $db = $db->whereBetween('h.exchange_time',[$requestData['start_exchange_time'],$requestData['end_exchange_time']]);
        }
        if (!empty($requestData['start_use_time']) && !empty($requestData['end_use_time'])){
            $db = $db->whereBetween('h.use_time',[$requestData['start_use_time'],$requestData['end_use_time']]);
        }

        if (!empty($requestData['invalid_time'])){
            $db = $db->where('h.end_use_time','<',$requestData['invalid_time']);
        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])) {
            $db = $db->order($requestData['order_type'], $requestData['sort_type']);
        } else {
            $db = $db->order('h.id', 'desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('u.headimgurl,u.nickname,u.code,u.tel,h.honghao_batch,h.be_from,h.is_use,h.exchange_time,h.use_time,h.end_use_time')
            ->page(input('page', 1), input('limit', 20))
            ->select();
        $data['total_num'] = $db1->count();
        foreach ($data['data'] as $k => $v){
            $data['data'][$k]['nickname'] = !empty($v['nickname']) ? CommonService::emoji_decode($v['nickname']) : '';
            switch ($v['is_use']) {
                case 0:
                    $data['data'][$k]['is_use'] = '未使用';
                    break;
                case 1:
                    $data['data'][$k]['is_use'] = '已使用';
                    break;
                case 2:
                    $data['data'][$k]['is_use'] = '强制失效';
                    break;
            }
            if ($v['end_use_time'] < time()){
                $data['data'][$k]['is_use'] = '过期失效';
            }
            switch ($v['be_from']) {
                case 1:
                    $data['data'][$k]['be_from'] = '金币兑换';
                    break;
                case 2:
                    $data['data'][$k]['be_from'] = '新人注册';
                    break;
                case 3:
                    $data['data'][$k]['be_from'] = '连续签到';
                    break;
                case 4:
                    $data['data'][$k]['be_from'] = '临时补偿';
                    break;
            }

        }
    }

    /**
     * 红包强制失效（is_use字段增加了 ：2 强制失效    可多选）
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editUseHongbao(Request $request)
    {
        $requestData = $request->post();
        $validate = Validate::make([
            'uid|用户id' => 'require',
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $uids = explode(',',$requestData['uid']);
        foreach ($uids as $k => $v){
            $data[$k] = [
                'uid' => $v,
                'is_use' => 2
            ];
        }
        Db::name('hongbao')->saveAll($data);
    }

    private function getHongbao($uid, $isUse = null)
    {
        if ($isUse = null){
            $data = Db::name('hongbao')->where('uid',$uid)->sum('hongbao_money');
        } else {
            $data = Db::name('hongbao')->where('uid',$uid)->where('is_use',$isUse)->sum('hongbao_money');
        }
        return $data;
    }
}